package com.l.d.app.library.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;

import com.l.d.app.library.R;
import com.l.d.app.library.util.ResourceUtil;

import skin.support.widget.SkinCompatView;

/**
 * Created by linde on 2017/5/2.
 * <p/>
 */
public class ShoppingCartIncreaseOrDecreaseView extends SkinCompatView {
    public static final int TYPE_INCREASE = 1;
    public static final int TYPE_DECREASE = 2;
    private int m_iType;

    private Paint mPaint;
    private int m_iColorBg;

    private int m_iColorTypeLine;
    private float m_fWidthTypeLine;

    private float m_fLinePadding;

    public ShoppingCartIncreaseOrDecreaseView(Context context) {
        this(context, null);
    }

    public ShoppingCartIncreaseOrDecreaseView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShoppingCartIncreaseOrDecreaseView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        m_iColorBg = ResourceUtil.color(context, R.color.colorAccent);

        m_iColorTypeLine = ResourceUtil.color(context, android.R.color.white);
        m_fWidthTypeLine = ResourceUtil.dimensionPixelSize(context, R.dimen.dimen2dp);

        m_fLinePadding = ResourceUtil.dimensionPixelSize(context, R.dimen.dimen10dp);

        m_iType = TYPE_DECREASE;
        if (attrs != null) {
            final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ShoppingCartIncreaseOrDecreaseView);

            m_iColorBg = array.getColor(R.styleable.ShoppingCartIncreaseOrDecreaseView_shopping_bg_color, m_iColorBg);

            m_iColorTypeLine = array.getColor(R.styleable.ShoppingCartIncreaseOrDecreaseView_shopping_line_color, m_iColorTypeLine);
            m_fWidthTypeLine = array.getDimension(R.styleable.ShoppingCartIncreaseOrDecreaseView_shopping_line_width, m_fWidthTypeLine);

            m_fLinePadding = array.getDimension(R.styleable.ShoppingCartIncreaseOrDecreaseView_shopping_padding, m_fLinePadding);

            m_iType = array.getInt(R.styleable.ShoppingCartIncreaseOrDecreaseView_shopping_type, TYPE_INCREASE);

            array.recycle();
        }
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);
        if (canvas == null) {
            return;
        }
        final Context context = getContext();
        final int width = getWidth();
        final int height = getHeight();

        setBackgroundColor(ResourceUtil.color(context, android.R.color.transparent));
        checkPaint();

        switch (m_iType) {
            case TYPE_INCREASE:
                drawIncrease(canvas, width, height);
                break;
            case TYPE_DECREASE:
                drawDecrease(canvas, width, height);
                break;
            default:
                throw new RuntimeException("Type Error!!!");
        }
    }

    private void drawDecrease(final Canvas canvas, final int width, final int height) {
        if (canvas == null) {
            return;
        }
        final float wPadding = m_fWidthTypeLine / 2F;
        // 背景圆(椭圆)
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(m_iColorBg);
        RectF rectF = new RectF(0 + wPadding, 0 + wPadding, width - wPadding, height - wPadding);
        canvas.drawOval(rectF, mPaint);

        final int h_2 = height / 2;

        final float xLeft = m_fLinePadding + wPadding;
        final float xRight = width - m_fLinePadding - wPadding;

        checkPaint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(m_iColorTypeLine);

        canvas.drawLine(xLeft, h_2, xRight, h_2, mPaint);
    }

    private void drawIncrease(final Canvas canvas, final int width, final int height) {
        if (canvas == null) {
            return;
        }
        final float wPadding = m_fWidthTypeLine / 2F;
        // 背景圆(椭圆)
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(m_iColorBg);
        RectF rectF = new RectF(0 + wPadding, 0 + wPadding, width - wPadding, height - wPadding);
        canvas.drawOval(rectF, mPaint);

        final int w_2 = width / 2;
        final int h_2 = height / 2;

        final float xLeft = m_fLinePadding + wPadding;
        final float xRight = width - m_fLinePadding - wPadding;
        final float yTop = m_fLinePadding + wPadding;
        final float yBottom = height - m_fLinePadding - wPadding;

        checkPaint();
        mPaint.setColor(m_iColorTypeLine);

        canvas.drawLine(xLeft, h_2, xRight, h_2, mPaint);
        canvas.drawLine(w_2, yTop, w_2, yBottom, mPaint);
    }

    private void checkPaint() {
        if (mPaint == null) {
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setStrokeWidth(m_fWidthTypeLine);
        }
    }

    @Override
    public void applySkin() {
        super.applySkin();
    }
}
